package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.dto.LaboratoryLinkmanDto;
import com.zito.health.entity.Unit;
import com.zito.health.entity.User;
import com.zito.health.service.UnitService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.UnitVo;
import com.zito.health.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: gxt
 * @time: 2019/11/8 14:55
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    @GetMapping
    public List<Unit> list(Unit unit){
        return unitService.query(unit);
    }

    @PostMapping("/currentUnit")
    public Unit currentUserUnit(){
        User current = CacheUtil.getCacheUser().getUser();
        return unitService.queryById(current.getUnitId());
    }
    @GetMapping("/getList")
    public PageResult list(Unit param, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult<UnitVo> result = new PageResult();
        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<UnitVo> list = unitService.queryList(param);
        PageInfo<UnitVo> pageInfo = new PageInfo<UnitVo>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return result;
    }
    @PostMapping
    public VerifyResult save( Unit unit) {
        VerifyResult verifyResult = new VerifyResult();
        verifyResult.setMsg("保存失败！");
        verifyResult.setResult(false);
        if(unit != null){
            Unit u =new Unit();
            u.setName(unit.getName());
            u = unitService.queryOne(u);
            if(u!=null){
                verifyResult.setMsg("该单位名已存在！");
                return verifyResult;
            }
            Unit unitRes =unitService.save(unit);
            if(unitRes!=null){
                verifyResult.setResult(true);
                verifyResult.setMsg("保存成功！");
            }
        }
        return verifyResult;
    }

    @PutMapping
    public VerifyResult update(Unit unit){
        VerifyResult verifyResult = new VerifyResult();
        Unit u= new Unit();
        u.setName(unit.getName());
        u.setId(unit.getId());
        List<Unit> list= unitService.validateUnitname4Update(u);
        if(list==null){
            verifyResult.setMsg("该单位已被删除！");
            verifyResult.setResult(false);
            return verifyResult;
        }else if(list.size()>0){
            verifyResult.setMsg("该单位名已被其他单位占用！");
            verifyResult.setResult(false);
            return verifyResult;
        }
        unitService.update(unit);
        return verifyResult;
    }

    @GetMapping("/{id}")
    public Unit queryById(@PathVariable Integer id){
        Unit laboratory = unitService.queryById(id);
        return laboratory;
    }

    @DeleteMapping("/{id}")
    public VerifyResult delete(@PathVariable Integer id){
        VerifyResult verifyResult = new VerifyResult();
        unitService.delete(id);
        return verifyResult;
    }

    /**
     * 验证单位名称
     * @param unitName
     * @return
     */
    @GetMapping("/validateUnitname")
    public VerifyResult validateUnitname(String unitName){
        VerifyResult verifyResult = new VerifyResult();
        Unit param = new Unit();
        param.setName(unitName);
        Unit unit=unitService.queryOne(param);
        if(unit==null){
            verifyResult.setResult(Boolean.FALSE);
        }
        return verifyResult;
    }

}
